
///模拟json数据


//  消息提示框////////////////////////////////////////////////////////////////////
function messageBox(content,state) {
    toastr.options = {
        closeButton: false,
        debug: false,
        progressBar: false,
        positionClass: "toast-top-right",
        onclick: null,
        showDuration: "300",
        hideDuration: "1000",
        // timeOut: "1000",  
        extendedTimeOut: "1000",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut"
    };
    if (content == null) {
        content = '';
    }
    var len = content.length;
    if (len <= 10 && len > 0) {
        toastr.options.timeOut = "1800";
    } else if (len <= 20) {
        toastr.options.timeOut = "2800";
    } else if (len <= 30) {
        toastr.options.timeOut = "3800";
    } else if (len > 30) {
        toastr.options.timeOut = "4800";
    }
    if(state == 1)
        toastr.success(content);
    else if(state == 2)
        toastr.error(content);
    else if(state == 3)
        toastr.warning(content);
    else
    toastr.info(content);
}
////////////////////////////////////////



// 个人信息、修改密码、退出登录下拉列表
$('.nav').children().click(function () {
    $(this).addClass('active')
    $(this).siblings().removeClass('active')
});

$('.topbar-right').click(function () {
    $(this).toggleClass('open')
});


//  修改密码
$('#changePasswordaa').click(function(){
    let data = {
        password:$('#password').val(),
        newPassword:$('#newPassword').val(),
        confirmPassword:$('#confirmPassword').val(),

    }


    $.ajax({
        url:'/user/ChangePassword',
        type:'GET',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:data,
        success:function(data){

        },
        error:function(){

        }
    })
})

  function xuanke(){
         $('.xuanke_1').click(function () {
        data1=$(this).parent().siblings().eq(0).text();
        console.log(data1);
        $.ajax({
            url: '/select_one/',
            type: 'get',
            dataType: 'json',
            data: {'data':data1},
            success: function (data) {

            },
            error: function () {

            }
        })
    })
    }
$('.topbar-left').on('click',function(){
    $('body').toggleClass('body-sidebar-hide')
})

//每次打开页面判单是那个html文件//向不同的路由发送ajax请求
if($('body').attr('id') == 'courseselection'){//自主选课
    console.log('自主选课')
    $.ajax({
        url:'/select/',
        type:'get',
        dataType:'json',
        data:{},
        success:function(data){
            console.log(data);
            for(var j in data){

                $('#charuya').append('   <tr>\n' +
                    '    <td>' + data[j].id + '</td>\n' +
                    '    <td>' + data[j].course1 + '</td>\n' +
                    '    <td>' + data[j].week + '</td>\n' +
                      '    <td>' + data[j].Duration + '</td>\n' +
                     '    <td>' + data[j].start + '</td>\n' +
                      '    <td>' + data[j].end + '</td>\n' +
                     '    <td>' + data[j].teacher + '</td>\n' +
                      '    <td>' + data[j].place + '</td>\n' +
                    '    <td class="shanchule"><button class="btn btn-primary xuanke_1">选课</button></td>\n' +
                    '</tr>');
            }
            xuanke()
        },
        error:function(){

        }
    })

    $('.radio-inline').children().on('click',function(){
        if($(this).val() == '选修课')
        {
            //ajax
        }
        else{
            //ajax
        }
    })

    // page(1)
}else if($('body').attr('id') == 'coursetimetable'){//课表查询
    console.log('课表查询')
    $.ajax({
        url:'/subject_1/',
        type:'get',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:{},
        success:function(data){
                  console.log(data);
            for(var j in data){

                $('#charuya').append('   <tr>\n' +
                    '    <td>' + data[j].id + '</td>\n' +
                    '    <td>' + data[j].course1 + '</td>\n' +
                    '    <td>' + data[j].week + '</td>\n' +
                      '    <td>' + data[j].Duration + '</td>\n' +
                     '    <td>' + data[j].start + '</td>\n' +
                      '    <td>' + data[j].end + '</td>\n' +
                     '    <td>' + data[j].teacher + '</td>\n' +
                      '    <td>' + data[j].place + '</td>\n' +
                    '    <td class="shanchule"><button class="btn btn-primary">选课</button></td>\n' +
                    '</tr>');
            }
        },
        error:function(){

        }
    })
}else if($('body').attr('id') == 'queryscore'){//成绩查询
    console.log('成绩查询')
    $.ajax({
        url:'/course_1/',
        type:'get',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:JSON.stringify(""),
        success:function(data){
                        console.log(data);
            for(var j in data){

                $('#charuya').append('   <tr>\n' +
                    '    <td>' + data[j].id + '</td>\n' +
                    '    <td>' + data[j].sub + '</td>\n' +
                    '    <td>' + data[j].teacher + '</td>\n' +
                      '    <td>' + data[j].chongxiu + '</td>\n' +
                     '    <td>' + data[j].chengji1 + '</td>\n' +

                    '    <td class="shanchule"><button class="btn btn-primary">选课</button></td>\n' +
                    '</tr>');
            }
        },
        error:function(){

        }
    })
}else if($('body').attr('id') == 'userinformation'){//用户信息
    console.log('用户信息')
    $.ajax({ //ajax请求用户信息
        url:'',
        type:'post',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:JSON.stringify(""),
        success:function(data){
            
        },
        error:function(){

        }
    })
    // 修改按钮点击事件
    $('#modify-information').click(function(){
        if($(this).text() != '保存')
        {
            $(this).siblings().children().removeAttr("disabled");
            $(this).text('保存')
        }
        else{
            $(this).siblings().children().attr("disabled",'disabled');
            $(this).text('修改')
            $.ajax({
                url:"",
                type:"post",
                contentType:'application/json; charset=UTF-8',
                data:JSON.stringify(''),
                dataType:'json',
                success:function (data) {
                },
                error:function (data) {
                }
            })
        }
    })
}else if($('body').attr('id') == 'teacher-userinformation'){//用户信息-老师
		$.ajax({  //ajax请求用户信息--老师
		        url:'/teacherinformation',
		        type:'get',
		        dataType:'json',
		        success:function(data){
		            console.log(data)
					$('#name').val(data[0]['name']);
					$('#email').val(data[0]['email']);
					$('#phone').val(data[0]['phone']);
					$('#address').val(data[0]['address']);
		        },
		        error:function(){
					console.log('shibai')
		        }
		    });

    // 修改按钮点击事件
    $('#modify-information').click(function(){
        if($(this).text() != '保存')
        {
            $(this).siblings().children().removeAttr("disabled");
            $(this).text('保存')
        }
        else{
            $(this).siblings().children().attr("disabled",'disabled');
            $(this).text('修改');
			var information = {
				name:$('#name').val(),
				email:$('#email').val(),
				phone:$('#phone').val(),
				address:$('#address').val()
			};
			$.ajax({
			    url:"/changeteacherinformation",
			    type:"post",
			    contentType:'application/json; charset=UTF-8',
			    data:JSON.stringify(information),
			    dataType:'json',
			    success:function (data) {
			    },
			    error:function (data) {
			    }
			})
        }
    })
}else if($('body').attr('id') == 'teacher-coursetimetable'){//教室课表
    console.log('教室课表')

    $.ajax({
        url:'/teacher/List',
        type:'get',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:JSON.stringify(""),
        success:function(data){
            console.log(data['data'][1])


           var html = "";
		    for(var i = 0;i < data['data'].length;i ++) {
				var p = data['data'][i];
				html += '<tr><td style="color:red;">' + p.teacher +
					'</td><td>' + p.place + '</td><td>' + p.week +
					'</td><td>' + p.Duration + '</td><td>' + p.start +
					'</td><td>' + p.end+ '</td></tr>';
				var tbody = document.getElementById("tbody");
				tbody.innerHTML = html;
			}


        },
        error:function(){

        }
    })
}else if($('body').attr('id') == 'teacher-queryscore'){//学生成绩评价
    console.log('学生成绩评价')
    $.ajax({
        url:'/teacher/evaluate',
        type:'get',
        dataType:'json',
        contentType:'application/json; charset=UTF-8',
        data:JSON.stringify(""),
        success:function(data){


           var html = "";
		    for(var i = 0;i < data['data'].length;i ++) {
				var p = data['data'][i];

				html += '<tr><td style="color:red;">' + p.id +
					'</td><td>' + p.student_name + '</td><td>' + p.sex +
					'</td><td>' + p.email + '</td><td>' + p.phone +
                    '</td><td>' + p.chengji1 + '</td><td>' + p.chongxiu+
					'</td><td>  <button type="button" class="butn"  subject_id=' + p.subject_id + ' >编辑</button></td></tr>';
				var tbody = document.getElementById("tbody1");
				tbody.innerHTML = html;

			}
    $('.butn').on('click', function () {
      let id = $(this).attr('subject_id')
    $.confirm({

        title: '评分',content: '' +
        '<form action="" class="formName">' +
        '<div class="form-group">' +
        '<label>请输入您对该学生的评分</label>' +
        '<input id="achi" type="text" placeholder="评分" class="name form-control" required />' +
        '</div>' +
        '</form>',buttons: {
            formSubmit: {
                text: '提交',
                btnClass: 'btn-blue',
                action: function () {
                    var name = this.$content.find('.name').val();

                    let data = {
                            id:id,
                            achievement:$('#achi').val(),
                    }
                    $.ajax({  //ajax请求学生成绩
                        url:'/teacher/evement',
                        type:'get',
                        data:data,
                        dataType:'json',
                        success:function(data){
                            window.location.reload()

                        },
                        error:function(){
                            console.log('shibai')
                        }
		    });

                }
            },cancel: {
                text: '取消'
            },
        }
    });
});
        },


        error:function(){

        }
    })
}



//分页
function page(currentPage){
    $('.table > tbody').empty()
    currentPage = (currentPage==undefined)?1:currentPage;
    var totalRow = userData.length;
    var pageRow = '10';
    var startRow = (currentPage - 1)*pageRow;
    var endRow = (currentPage * pageRow);endRow = (endRow>totalRow)?totalRow:endRow;
    if(totalRow/pageRow > parseInt(totalRow/pageRow))
        totalPage = parseInt(totalRow/pageRow)+1;
    else
        totalPage = parseInt(totalRow/pageRow);

    for(var i=startRow;i<endRow;i++)
    {
        userData[i]['name']
        var temp = $("<tr>"+"<td>"+userData[i]['name']+"</td>"+"<td>"+userData[i]['place']+"</td>"+"<td>"+userData[i]['teacher']+"</td>"+"<td>"+userData[i]['timer']+"</td>"+"<td>"+userData[i]['number']+"</td>"+"<td><button class='btn btn-primary'>选课</button></td>"+"</tr>")
        $('.table > tbody').append(temp)
    }

    var str = '';
    if(currentPage>1)
        str += '<li><a href="javascript:void(0)" onClick=page('+(currentPage-1)+')><i class="fa fa-angle-left" aria-hidden="true"></i></a></li>';
    else
        str += '<li class="disabled"><a href="javascript:void(0)"><i class="fa fa-angle-left" aria-hidden="true"></i></a></li>';

    if(totalPage<=5)
    {
        for(var i=1;i<=totalPage;i++)
        {
            if(i==currentPage)
                str += '<li class="active"><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
            else
                str += '<li><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
        }
    }
    else{
           if(currentPage > 1)
           {
                if(totalPage-3 <= currentPage)
                {
                    for(var i=totalPage-4;i<=totalPage;i++)
                    {
                        if(i==currentPage)
                            str += '<li class="active"><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
                        else
                            str += '<li><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
                    }
                }
                else{
                    for(var i=currentPage-1;i<=currentPage+1;i++)
                    {
                        if(i==currentPage)
                            str += '<li class="active"><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
                        else
                            str += '<li><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
                    }
                    str += '<li><a href="javascript:void(0)">...</a></li>';
                    str += '<li><a href="javascript:void(0)" onClick=page('+(totalPage)+')>'+(totalPage)+'</a></li>';
                }
           }
           else{
               for(var i=currentPage;i<=currentPage+2;i++)
               {
                if(i==currentPage)
                    str += '<li class="active"><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
                else
                    str += '<li><a href="javascript:void(0)" onClick=page('+(i)+')>'+(i)+'</a></li>';
               }
               str += '<li><a href="javascript:void(0)">...</a></li>';
               str += '<li><a href="javascript:void(0)" onClick=page('+(totalPage)+')>'+(totalPage)+'</a></li>';
           }
    }

    if(totalPage > currentPage)
        str += '<li><a href="javascript:void(0)" onClick=page('+(currentPage+1)+')><i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';
    else
        str += '<li class="disabled"><a href="javascript:void(0)"><i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';

    $('.pagination').html(str)
}

